"""
Author  :lee
Date    :2024/2/28 18:21
Desc    :
"""
# 创建了新的tags标签文件后必须重启服务器
from datetime import datetime


from django import template
from ..models import (
    Article,
    Category,
    Tag,
    FriendLink,
)

from django.db.models.aggregates import Count
from django.utils.html import mark_safe
import re

register = template.Library()


# 文章相关标签函数
@register.simple_tag
def get_article_list(sort=None, num=None):
    """获取指定排序方式和指定数量的文章"""
    if sort:
        if num:
            return Article.objects.filter(is_publish=True).order_by(sort)[:num]
        return Article.objects.filter(is_publish=True).order_by(sort)
    if num:
        return Article.objects.filter(is_publish=True)[:num]
    return Article.objects.filter(is_publish=True)


@register.simple_tag
def get_instance_article_list(instance):
    """
    获取一个专题下所有文章，支持分类，tag，subject, topic
    @param instance: 实例
    @return:
    """
    model_class = instance._meta.model
    if model_class.__name__ == 'Category':
        return Article.objects.filter(category=instance, is_publish=True)
    elif model_class.__name__ == 'Tag':
        return Article.objects.filter(tags=instance, is_publish=True)
    elif model_class.__name__ == 'Topic':
        return Article.objects.filter(topic=instance, is_publish=True)


@register.simple_tag
def keywords_to_str(art):
    """将文章关键词变成字符串"""
    keys = art.keywords.all()
    return ','.join([key.name for key in keys])


@register.simple_tag
def get_tag_list():
    """返回标签列表"""
    return Tag.objects.filter(article__is_publish=True).annotate(
        total_num=Count('article')).filter(total_num__gt=0)


@register.simple_tag
def get_category_list():
    """返回分类列表"""
    return Category.objects.filter(article__is_publish=True).annotate(
        total_num=Count('article')).filter(total_num__gt=0)


@register.simple_tag
def get_new_article_id():
    """得到最后一个article的id，用来设置到缓存里面，实现动态缓存"""
    objects = Article.objects.order_by('-pk')
    return objects.first().pk if objects else 0


@register.simple_tag
def get_star(num):
    """得到一排星星"""
    tag_i = '<i class="fa fa-star"></i>'
    return mark_safe(tag_i * num)


@register.simple_tag
def get_star_title(num):
    """得到星星个数的说明"""
    the_dict = {
        1: '【1颗星】：微更新，涉及轻微调整或者后期规划了内容',
        2: '【2颗星】：小更新，小幅度调整，一般不会迁移表格',
        3: '【3颗星】：中等更新，一般会增加或减少模块，有表格的迁移',
        4: '【4颗星】：大更新，涉及到应用的增减',
        5: '【5颗星】：最大程度更新，一般涉及多个应用和表格的变动',
    }
    return the_dict[num]


@register.simple_tag
def my_highlight(text, q):
    """自定义标题搜索词高亮函数，忽略大小写"""
    if len(q) > 1:
        try:
            text = re.sub(q, lambda a: '<span class="highlighted">{}</span>'.format(a.group()),
                          text, flags=re.IGNORECASE)
            text = mark_safe(text)
        except:
            pass
    return text


@register.simple_tag
def get_request_param(request, param, default=None):
    """获取请求的参数"""
    return request.POST.get(param) or request.GET.get(param, default)


@register.simple_tag
def get_friends(is_show=True, is_active=True):
    """获取友情链接"""
    return FriendLink.objects.filter(is_show=is_show, is_active=is_active)


@register.simple_tag
def now_hour():
    """返回当前时间的小时数"""
    return datetime.now().hour


@register.filter(is_safe=True)
def my_slice(value, arg):
    """
    复制内置的标签函数，截断字符串，并给字符串添加...
    {{ friend.description|my_slice:":23" }}
    """
    try:
        bits = []
        for x in str(arg).split(':'):
            if not x:
                bits.append(None)
            else:
                bits.append(int(x))
        result = value[slice(*bits)]
        if isinstance(value, str):
            # 超过长度则在后面补...
            if len(value) > bits[-1]:
                result += '...'
            # 不是从头开始，则在前面补...
            if bits[0]:
                result = '...' + result
        return result

    except (ValueError, TypeError):
        return value

